<template>
  <!-- 图纸会审/技术洽商 -->
  <base-breadcrumb class="allPlan">
    <a-card>
      <a-row :gutter="10" class="page-height">
        <a-col :span="4" style="border-right: 1px solid #d9d9d9">
          <a-card :bordered="false" class="page-height-tree">
            <title-name title="项目列表"></title-name>
            <!-- 公司组织树搜索 -->
            <PermissionTreeOrganization :collapsed="false" @select="handleSelect" />
            <!-- <tree-project
                          ref="treeOrganization"
                          @loader="search"
                          :selectedKeys.sync="selectedKeys"
                          :selectOptions.sync="selectOptions"
                      ></tree-project> -->
          </a-card>
        </a-col>
        <a-col :span="20">
          <searchData :searchData="searchArr" @search="search"></searchData>
      <!-- <title-name title="图纸列表"></title-name> -->
      <!-- 表格 -->
      <list-table
        ref="table"
        :columns="columns"
        :startIndex="1"
        :request="loadData"
        :outSelectedRowKeys.sync="selectedKeys"
        :exportConfig="{
          use: true,
          select: $route.meta.title,
          all: $route.meta.title,
          customColumns: customColumns
        }"
      >
        <!-- 新增 -->
        <a-button
          v-btnPermission="'tm_021_add_01'"
          slot="prefix"
          class="mb12"
          icon="plus"
          type="primary"
          @click="handleManage(2)"
          :disabled="selectOptions.tageCode !== 'PD'"
          >新增</a-button
        >
        <a-button
                v-btnPermission="'tm_020_add_01'"
                slot="prefix"
                class="mb12"
                icon="download"
                type="primary"
                :disabled="!selectedKeys.length"
                @click="batchExportFiles"
        >批量导出附件</a-button
        >
        <span slot="operation" slot-scope="text, record">
          <MoreButton>
          <!-- <MoreButton> -->
          <a v-btnPermission="'tm_021_loo_04'" @click="handleManage(-1, record)">查看</a>
          <!-- 编辑 -->
          <span
            v-if="record.status == 'DRAFT' || record.status == 'REJECTED'"
            v-btnPermission="'tm_021_edi_02'"
          >
            <a @click="handleManage(record.status === 'PAST' ? -1 : 1, record)">编辑</a>
          </span>

          <template v-if="record.status == 'REJECTED' || record.status == 'PAST'">
              <span
                style="color: #1890ff;"
                v-if="record.status == 'REJECTED'"
                v-btnPermission="'tm_021_del_03'"
              >
                <a @click="handleRemoveParams({ id: record.id, type: 1 })">删除</a>
              </span>
              <!-- 作废 -->
              <a
                style="color: #1890ff;"
                v-if="record.status == 'PAST'&&record.drawEnum === 'IMPLEMENT'"
                v-btnPermission="'tm_021_tov_05'"
                @click="handleInvalid({ id: record.id, type: 2 })"
              >
                作废
              </a>
              <!-- 审批 -->
              <span v-if="record.status !== 'DRAFT'" v-btnPermission="'tm_021_loo_04'">
                <approval-button :id="record.id" :status="record.status" /> </span
            >
          </template>
          <template v-else>
            <!-- 删除 -->
            <span v-if="record.status == 'DRAFT' || record.status == 'REJECTED'" v-btnPermission="'tm_021_del_03'">
              <a @click="handleRemoveParams({ id: record.id, type: 1 })">删除</a>
            </span>
            <!-- 审批 -->
            <span v-if="record.status !== 'DRAFT'" v-btnPermission="'tm_021_loo_04'">
              <approval-button :id="record.id" :status="record.status" />
            </span>
          </template>
            </MoreButton>
        </span>

        <span slot="status" slot-scope="text, record">
          <list-status :status="record.status" :isApproval="true" conversion="en" :text="text"></list-status>
        </span>
      </list-table>
        </a-col>
      </a-row>
    </a-card>
    <!-- </a-col>
    </a-row> -->
  </base-breadcrumb>
</template>

<script>
import BasePage from '@/views/basePage' // 基础配置
import { getDrawChangeList, delDrawChange } from '@/api/technical/designFM'
import TreeProject from '@/components/businessComponents/TreeProject' // 树
import Selector from '@/components/Select/Selector'
import MoreButton from '@/components/MoreButton'
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
import SelectProject from '@/components/Select/SelectProject' // 项目
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司s
import OrgTreeSelect from '@/components/OrgTreeSelect'
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '所属分公司',
    dataIndex: 'branchComName',
    ellipsis: true
  },
  {
    title: '所属项目',
    dataIndex: 'projectName',
    ellipsis: true
  },
  {
    title: '专业类别',
    dataIndex: 'professionalTypeName',
    ellipsis: true
  },
  {
    title: '工程部位',
    dataIndex: 'projectLocation',
    ellipsis: true
  },
  {
    title: '文件名称',
    dataIndex: 'name',
    ellipsis: true
  },
  {
    title: '文件编号',
    dataIndex: 'num',
    ellipsis: true
  },
  {
    title: '会审时间',
    dataIndex: 'talksDate',
    ellipsis: true
  },
  {
    title: '收文日期',
    dataIndex: 'receiptDate'
  },
  {
    title: '发文部门',
    dataIndex: 'sendDepartment',
    ellipsis: true
  },

  {
    title: '是否涉及返工',
    dataIndex: 'reworkStr'
  },
  {
    title: '是否涉及索赔',
    dataIndex: 'claimStr'
  },
  {
    title: '实施状态',
    dataIndex: 'drawEnumStr'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    width: '180px',
    scopedSlots: { customRender: 'operation' }
  }
]

import { STable } from '@/components'
import { batchExportFiles, majorTypeSelectListTree } from '@/api/technology'
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    STable,
    TreeProject,
    MoreButton,
    PermissionTreeOrganization
  },
  data() {
    this.columns = columns

    return {
      selectOptions: {},
      // 统计
      statistics: {
        bluePicture: 0,
        deepPicture: 0,
        otherInteger: 0,
        totalPicture: 0,
        whitePicture: 0
      },

      selectedKeys: [],
      queryParam: {},
      removeApi: { remove: delDrawChange, invalid: delDrawChange, undo: delDrawChange }, // api

      loadData: async parameter => {
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        return getDrawChangeList(requestParameters).then(res => {
          res.data.forEach(item=>{
            if(item.changeReviewEnum === 'REVIEWER') {
              item.drawEnumStr = ''
            }
          })
          return res
        })
      },
      customColumns: [
        { title: '序号', dataIndex: 'index' },
        { title: '所属分公司', dataIndex: 'branchComName' },
        { title: '所属项目', dataIndex: 'projectName' },
        { title: '专业类别', dataIndex: 'professionalTypeName' },
        {
          title: '工程部位',
          dataIndex: 'projectLocation'
        },
        { title: '文件名称', dataIndex: 'name' },
        { title: '文件编号', dataIndex: 'num' },
        { title: '会审时间', dataIndex: 'talksDate' },
        { title: '收文日期', dataIndex: 'receiptDate' },
        { title: '发文部门', dataIndex: 'sendDepartment' },
        { title: '是否涉及返工', dataIndex: 'reworkStr' },
        { title: '是否涉及索赔', dataIndex: 'claimStr' },
        { title: '实施状态', dataIndex: 'drawEnumStr' },
        { title: '状态', dataIndex: 'statusStr' },

      ],
      searchArr: [
        {
          name: '专业类别',
          type: 'tree-select',
          value: 'professionalTypeId',
          attrs: {
            replaceFields: {children:'children', title:'majorName', key:'id', value: 'id' },
            showSearch: true,
            treeNodeFilterProp: 'title'
          },
          data:[]
        },
        {
          name: '文件名称',
          type: 'input',
          value: 'name'
        },
        {
          name: '文件编号',
          type: 'input',
          value: 'num'
        },
        {
          name: '会审时间',
          type: 'dateRange',
          value: 'talksDate',
          attr: {
            rangeFormat: ['talksDateBegin', 'talksDateEnd']
          }
        },
        {
          name: '收文日期',
          type: 'dateRange',
          value: 'callNameDate',
          attr: {
            rangeFormat: ['receiptBeginDate', 'receiptEndDate']
          }
        },
        {
          name: '发文部门',
          type: 'input',
          value: 'sendDepartment'
        },
        {
          name: '工程部位',
          type: 'input',
          value: 'projectLocation'
        },
        {
          name: '类型',
          value: 'changeReviewEnum',
          type: 'select',
          data:[
            {
              value: 'TALKS',
              label: '工程技术洽商单'
            },
            {
              value: 'REVIEWER',
              label: '图纸会审记录'
            }
          ]
        }
      ]
    }
  },
  created() {
    this.majorTypeSelectListTree()
  },
  methods: {
    batchExportFiles() {
      const data = {
        ids:this.selectedKeys,
        typeId: 1,
        urlStr: window.location.origin
      }
      batchExportFiles(data).then(res=>{
        this.handleDownload(res)
      })
    },
    /**
     * 转义下载
     * @param parameter
     */
    handleDownload(res) {
      const blob = new Blob([res.data])
      const filename = decodeURIComponent(res.headers['content-disposition'].split('filename=')[1])
      console.log('filenamefilename',filename)
      const downloadElement = document.createElement('a')
      const href = window.URL.createObjectURL(blob) // 创建下载的链接
      downloadElement.href = href
      downloadElement.download = filename.substring(filename.indexOf("''") + 1) // 获取文件名
      document.body.appendChild(downloadElement)
      downloadElement.click() //点击下载
      document.body.removeChild(downloadElement) //下载完成移除元素
      window.URL.revokeObjectURL(href) //释放blob对象
    },
    handleSelect(node) {
      this.selectOptions = node
      this.search()

    },
    majorTypeSelectListTree() {
      majorTypeSelectListTree().then(res=>{
        res.data.forEach(item=>{
          item.majorName = item.programmeName
          item.disabled = true
        })
        this.searchArr[0].data = res.data
      })
    },
    //格式化当前日期
    formatDate(date, type = '-') {
      let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return date.getFullYear() + type + M + type + D
    },

    //搜索
    search(value) {
      value && (this.queryParam = value)
      console.log(this.selectOptions)
      if (this.selectOptions.tageCode == 'PD') {
        this.queryParam.projectId = this.selectOptions.projectId
      } else if (this.selectOptions.tageCode == 'BC') {
        this.queryParam.branchComId = this.selectOptions.orgId
      } else {
        delete this.queryParam.projectId
        delete this.queryParam.branchComId
      }
      this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      let query = {
        editModel: type == -1 ? 0 : type,
        comment: type == -1,
        projectId: this.selectOptions.projectId
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: 'updateDrawingAndNegotiation',
        query
      })
    }
  }
}
</script>
